package com.example.back.Repository;

import com.example.back.Entity.Appointment;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public interface AppointmentRepository extends JpaRepository<Appointment, Long> {
    Page<Appointment> findAllByAppointmentUserId(long UserId, Pageable pageable);

    Appointment findAppointmentByAppointmentId(long appId);

    Appointment findAppointmentByAppointmentAuctionIdAndAppointmentUserId(long auctionId, long userId);

    @Transactional
    @Modifying
    @Query(value = "delete from Appointment appointment where appointment.appointmentAuctionId=:auctionId and appointment.appointmentUserId=:userId")
    void deleteAppointmentByAU(long auctionId, long userId);

    @Transactional
    @Modifying
    @Query(value = "update Auction auction set auction.appointmentNum=auction.appointmentNum+1 where auction.auctionId=:aid")
    void addAppointment(long aid);

    @Transactional
    @Modifying
    @Query(value = "update Auction auction set auction.appointmentNum=auction.appointmentNum-1 where auction.auctionId=:aid")
    void decreaseAppointment(long aid);
}
